package com.example.campuslink.ui.fragment

import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.adpter.MainInvitationAdapter
import com.example.campuslink.commen.adpter.TopicTabAdapter
import com.example.campuslink.commen.enums.MineInvitationNowStatusEnum
import com.example.campuslink.commen.enums.TopicEnum
import com.example.campuslink.databinding.FragmentMainBinding
import com.example.campuslink.ui.activity.MainActivity
import com.example.campuslink.viewModel.MainFragmentViewModel
import java.lang.ref.WeakReference

class MainFragment : BaseFragment<FragmentMainBinding, MainFragmentViewModel>(
    MainFragmentViewModel::class.java,
    R.layout.fragment_main,
    BR.viewModel
) {
    private var activityRef: WeakReference<MainActivity>? = null
    private var adapter: MainInvitationAdapter? = null
    private var topicList: MutableList<String> = TopicEnum.getAllLabels().toMutableList()
    private var topicTabAdapter: TopicTabAdapter? = null
    override fun onResume() {
        super.onResume()
        if (mViewModel?.getInvitationType?.value == "关注") {
            mViewModel?.getAttentionInvitation()
        } else if (mViewModel?.getInvitationType?.value == "粉丝") {
            mViewModel?.getFansInvitation()
        } else {
            mViewModel?.getInvitation(topicList[mViewModel?.topicTableIndex?.value ?: 0])
        }
    }
    override fun onClick() {
        mViewDataBinding?.freshLayout?.setOnRefreshListener {
            if(mViewModel?.getInvitationType?.value=="关注"){
                mViewModel?.getAttentionInvitation()
            }else if(mViewModel?.getInvitationType?.value=="粉丝"){
                mViewModel?.getFansInvitation()
            }else{
                mViewModel?.getInvitation(topicList[mViewModel?.topicTableIndex?.value ?: 0])
            }
            mViewDataBinding?.freshLayout?.isRefreshing = false
        }
        mViewDataBinding?.ivMenu?.setOnClickListener({
            activityRef?.get()?.openDrawerLayout()
        })
        mViewDataBinding?.ivSearch?.setOnClickListener({
            mViewModel?.jumpToSearchActivity()
        })
        mViewDataBinding?.rgMainTab?.setOnCheckedChangeListener({ group, checkedId ->
            when (checkedId) {
                R.id.rb_attention -> {
                    mViewModel?.getAttentionInvitation()
                    mViewModel?.setGetInvitationType("关注")
                }

                R.id.rb_fans -> {
                    mViewModel?.getFansInvitation()
                    mViewModel?.setGetInvitationType("粉丝")
                }

                R.id.rb_discover -> {
                    mViewModel?.getInvitation(topicList[mViewModel?.topicTableIndex?.value ?: 0])
                    mViewModel?.setGetInvitationType("发现")
                }

            }
        })
    }

    override fun initViews() {
        mViewDataBinding?.rvInvitationShow?.adapter = adapter
        mViewDataBinding?.rvInvitationShow?.layoutManager = GridLayoutManager(requireContext(), 2)
        mViewModel?.schoolAllInvitation?.observe(this) {
            adapter?.setListData(it.reversed())
        }
        mViewDataBinding?.rvTab?.adapter = topicTabAdapter
        mViewDataBinding?.rvTab?.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)

        mViewModel?.getInvitationType?.observe(this){
            if(it=="关注"){
                mViewDataBinding?.rvTab?.visibility = View.GONE
            }else if(it=="粉丝"){
                mViewDataBinding?.rvTab?.visibility = View.GONE
            }else{
                mViewDataBinding?.rvTab?.visibility = View.VISIBLE
            }
        }

    }

    @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
    override fun initData() {
        mViewModel?.context = requireContext()
        mViewModel?.initDB(requireContext())
        topicList.add(0, "全部")
        mViewModel?.getInvitation(topicList[0])
        adapter = MainInvitationAdapter(mViewModel!!)
        topicTabAdapter = TopicTabAdapter(mViewModel!!)
        topicTabAdapter?.setListData(topicList)
        val activity = parentFragment?.activity ?: activity
        if (activity is MainActivity) {
            activityRef = WeakReference(activity)
        }
    }
}